package com.ruoyi.common.utils;


import org.slf4j.MDC;

import java.util.UUID;

/**
 * 日志跟踪工具类
 *
 * @author yanzhengao@jd.com
 * @date 2021/4/15
 */
public class MDCUtils {

    private static final String TRACE_KEY = "mdc_trace_key_channels";

    private MDCUtils(){}

    /**
     * @author yanzhengao@jd.com
     * 创建一个日志链路跟踪
     */
    public static void newTraceId() {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        MDC.put(TRACE_KEY, uuid);
    }

    /**
     * @author yanzhengao@jd.com
     * 获取当前线程的traceId
     *
     * @return traceId
     */
    public static String getTraceId(){
        return MDC.get(TRACE_KEY);
    }

}
